package com.longjianghuasheng.logistics.business.freight

import android.os.Bundle
import com.blankj.utilcode.util.ColorUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseFragment
import com.longjianghuasheng.logistics.business.detail.DetailFragment
import com.longjianghuasheng.logistics.business.freight.adapter.TransportListAdapter
import com.longjianghuasheng.logistics.business.inquiry.InquiryFragment
import com.longjianghuasheng.logistics.databinding.FragmentServiceListBinding
import com.longjianghuasheng.logistics.ext.observer
import com.xuexiang.xui.utils.WidgetUtils

class ServiceListFragment :
    BaseFragment<FreightViewModel, FragmentServiceListBinding>() {


    private val transportListAdapter by lazy {
        TransportListAdapter()

    }


    override fun layoutId(): Int = R.layout.fragment_service_list

    override fun initView(savedInstanceState: Bundle?) {


        mDatabind.recyclerView.run {
            setHasFixedSize(true)
            isNestedScrollingEnabled = true
            WidgetUtils.initRecyclerView(this, 1, ColorUtils.getColor(R.color.baseBackground))
            adapter = transportListAdapter

        }
        transportListAdapter.run {


            onListener = { id ->


                when (mViewModel.id) {

                    TYPE_IMPORT, TYPE_EXPORT, TYPE_DOMESTIC -> {
                        navigate(R.id.inquiryFragment, Bundle().apply {
                            putInt("id", id)
                            putInt(
                                InquiryFragment.KEY_TYPE,
                                mViewModel.id
                            )
                        })

                    }

                    TYPE_PARK_WAREHOUSING -> {
                        navigate(R.id.storageAndDisplayFragment, Bundle().apply {
                            putInt("id", id)
                        })
                    }
                    TYPE_SITE_LOADING_AND_UNLOADING,
                    TYPE_WAREHOUSING_AND_DISTRIBUTION, TYPE_BONDED,
                    TYPE_REPORT_FOR_INSPECTION
                    -> {
                        navigate(R.id.detailFragment, Bundle().apply {
                            putInt("id", id)
                            putBoolean(
                                "isTheSlingLoadingAndUnloading",
                                data.first { it.id == id }.type3 == 23
                            )
                            putInt(DetailFragment.KEY_TYPE, mViewModel.id)

                        })
                    }
//                    -> {
//                        navigate(R.id.detailFragment, Bundle().apply {
//
//                            putString(
//                                DetailFragment.KEY_TYPE,
//                                DetailFragment.TYPE_SHORT_DISTANCE_DETAIL
//
//
//                            )
//
//                        })
//                    }
//                    -> {
//                        navigate(R.id.detailFragment, Bundle().apply {
//
//                            putString(
//                                DetailFragment.KEY_TYPE,
//                                DetailFragment.TYPE_UNLOADING
//
//
//                            )
//
//                        })
//                    }


                    else -> {}
                }
//                navigate(R.id.detailFragment, Bundle().apply {
//
//                    putString(
//                        DetailFragment.KEY_TYPE, when (position) {
//                            0 -> DetailFragment.TYPE_SHORT_DISTANCE
//                            1 -> DetailFragment.TYPE_SHORT_DISTANCE_DETAIL
//                            2 -> DetailFragment.TYPE_UNLOADING
//                            else -> DetailFragment.TYPE_UNLOADING_DETAIL
//                        }
//                    )
//
//                })

            }


        }
    }


    override fun createObserver() {
        super.createObserver()
        mViewModel.freightResult.observer(this) {

            transportListAdapter.setNewInstance(it)
        }
    }


    override fun initData() {
        super.initData()
        arguments?.run {
            mViewModel.id = getInt("id")

        }


    }


    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.service_list2()

    }

    companion object {

        private const val TAG = "ServiceListFragment"


        fun newInstance(typeId: Int): ServiceListFragment {
            val fragment = ServiceListFragment()
            fragment.arguments = Bundle().apply {
                putInt("id", typeId)
            }
            return fragment
        }

        const val TYPE_IMPORT = 15 //进口服务
        const val TYPE_EXPORT = 16  // 出口服务
        const val TYPE_DOMESTIC = 17  //国内运输
        const val TYPE_PARK_WAREHOUSING = 2  //园区仓储
        const val TYPE_SITE_LOADING_AND_UNLOADING = 3 // 场地装卸场地装卸
        const val TYPE_WAREHOUSING_AND_DISTRIBUTION = 4 // 仓储配送服务
        const val TYPE_BONDED = 6 // 保税业务
        const val TYPE_REPORT_FOR_INSPECTION = 7 // 报关报检

    }

}